/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oop.example.work.esport;

import com.oop.example.processing.msdoc.HtmlPreparer;
import com.oop.example.processing.msdoc.LengthComparator;
import com.oop.example.utils.Utils;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import javax.xml.bind.JAXB;

/**
 *
 * @author -
 */
public class Esp2PortHtmlPreparer extends HtmlPreparer {

    public static final Map<String, String> portumap = new LinkedHashMap<String, String>();
    public static final Map<String, String> spanishmap = new LinkedHashMap<String, String>();

    static {
        portumap.put("&Agrave;", "À");
        portumap.put("&agrave;", "à");
        portumap.put("&Aacute;", "Á");
        portumap.put("&aacute;", "á");
        portumap.put("&Acirc;", "Â");
        portumap.put("&acirc;", "â");
        portumap.put("&Atilde;", "Ã");
        portumap.put("&atilde;", "ã");
        portumap.put("&Ccedil;", "Ç");
        portumap.put("&ccedil;", "ç");
        portumap.put("&Egrave;", "È");
        portumap.put("&egrave;", "è");
        portumap.put("&Eacute;", "É");
        portumap.put("&eacute;", "é");
        portumap.put("&Ecirc;", "Ê");
        portumap.put("&ecirc;", "ê");
        portumap.put("&Igrave;", "Ì");
        portumap.put("&igrave;", "ì");
        portumap.put("&Iacute;", "Í");
        portumap.put("&iacute;", "í");
        portumap.put("&Iuml;", "Ï");
        portumap.put("&iuml;", "ï");
        portumap.put("&Ograve;", "Ò");
        portumap.put("&ograve;", "ò");
        portumap.put("&Oacute;", "Ó");
        portumap.put("&oacute;", "ó");
        portumap.put("&Otilde;", "Õ");
        portumap.put("&otilde;", "õ");
        portumap.put("&Ugrave;", "Ù");
        portumap.put("&ugrave;", "ù");
        portumap.put("&Uacute;", "Ú");
        portumap.put("&uacute;", "ú");
        portumap.put("&Uuml;", "Ü");
        portumap.put("&uuml;", "ü");
        portumap.put("&ordf;", "ª");
        portumap.put("&ordm;", "º");
        portumap.put("&Ocirc;", "Ô");
        portumap.put("&ocirc;", "ô");

        spanishmap.put("&Aacute;", "Á");
        spanishmap.put("&aacute;", "á");
        spanishmap.put("&Eacute;", "É");
        spanishmap.put("&eacute;", "é");
        spanishmap.put("&Iacute;", "Í");
        spanishmap.put("&iacute;", "í");
        spanishmap.put("&Oacute;", "Ó");
        spanishmap.put("&oacute;", "ó");
        spanishmap.put("&Ntilde;", "Ñ");
        spanishmap.put("&ntilde;", "ñ");
        spanishmap.put("&Uacute;", "Ú");
        spanishmap.put("&uacute;", "ú");
        spanishmap.put("&Uuml;", "Ü");
        spanishmap.put("&uuml;", "ü");
        spanishmap.put("&iexcl;", "¡");
        spanishmap.put("&ordf;", "ª");
        spanishmap.put("&iquest;", "¿");
        spanishmap.put("&ordm;", "º");
    }

    public Esp2PortHtmlPreparer() {
        replacements.put("<b><span (class|style|lang)=" + commonChars + "color:aqua" + commonChars + ">", "<TEMPB style='color:aqua'>");
        replacements.put("<p class=" + commonChars + ">", "<p>");
        replacements.put("<b (style|lang)=" + commonChars + ">", "<b>");
        replacements.put("<span (class|style|lang)=" + commonChars + ">", "<span>");
        replacements.put("<i (style|lang)=" + commonChars + ">", "<i>");
        replacements.put("&#61448;", "&#769;");//причесываем ударения
        replacements.put("&#61449;", "&#769;");//причесываем ударения
        replacements.put("&#769;&#769;", "&#769;");//причесываем ударения
        replacements.put("</b>&#769;<b>", "&#769;");//причесываем ударения
        replacements.put("<b>&#769;</b>", "&#769;");//причесываем ударения
        replacements.put("</i>&#769;<i>", "&#769;");//причесываем ударения
        replacements.put("<i>&#769;</i>", "&#769;");//причесываем ударения
        replacements.put("</b><b>", "");//объединение нескольких <b>
        replacements.put("</i><i>", "");//объединение нескольких <i>
        replacements.put("<o:p></o:p>", "");//лишнее
        replacements.put("<span>", "");//лишнее
        replacements.put("</span>", "");//лишнее
        replacements.put("<br>", "");//лишнее
        replacements.put("<br/>", "");//лишнее
        replacements.put("</b>&#769;", "&#769;</b>");//возвращает "потерянные в хвосте" ударения
        replacements.put("  ", " ");//Пробелы двойные
        replacements.put("</b>, <b>", ", ");
        replacements.put("<u>", "");
        replacements.put("</u>", "");
        replacements.put("</i>\\)", ")</i>");
        replacements.put("\\(<i>", "<i>(");
        replacements.put("</i>\\.", ".</i>");
        replacements.put("</i> <i>", " ");
        replacements.put("</i><b>,</b> <i>", ", ");
        replacements.put("</i>&amp;", "&amp;</i>");
        replacements.put("&amp;<i>", "<i>&amp;");
        replacements.put("<p>", "");
        replacements.put("</p>", "");
        replacements.put(" </i>", "</i> ");
        replacements.put("<b>&acute;</b>", "´");
        replacements.put("&acute;", "´");
        replacements.put("&#952;", "θ");
        replacements.put("&#955;", "λ");
        replacements.put("&#643;", "ʃ");
        replacements.put("&#8747;", "ʃ");//∫
        replacements.put("&#772;", "̄");
        replacements.put("&#8594;", "→");
        replacements.put("→ ", "→");

        replacements.put("<b>, </b>", ", ");
        replacements.put("<b></b>", "");
        replacements.put("<i></i>", "");
        replacements.put("</i></i>", "</i>");
        replacements.put("<i><i>", "<i>");
        replacements.put("<i>,</i>", ",");
        replacements.put("<b>,</b>", ",");
        replacements.put("<i>´</i>", "´");
        replacements.put("</b> <b>", " ");
        replacements.put("<b>\\*</b>", "*");
        replacements.put("<i>\\.</i>", ".");
        replacements.put("<b>\\.</b>", ".");
        replacements.put("</i>\\, <i>", ", ");
        //
        replacements.put("<b>1</b>", "<b>@1</b>");
        replacements.put("<b>2</b>", "<b>@2</b>");
        replacements.put("<b>3</b>", "<b>@3</b>");
        replacements.put("<b>4</b>", "<b>@4</b>");
        replacements.put("<b>5</b>", "<b>@5</b>");
        replacements.put("<b>6</b>", "<b>@6</b>");
        replacements.put("<b>7</b>", "<b>@7</b>");
        replacements.put("<b>8</b>", "<b>@8</b>");
        replacements.put("<b>9</b>", "<b>@9</b>");
        replacements.put("<b>10</b>", "<b>@10</b>");
        replacements.put("<b>11</b>", "<b>@11</b>");
        replacements.put("<b>12</b>", "<b>@12</b>");
        //
        replacements.put("<b>1 </b>", "<b>@1</b> ");
        replacements.put("<b>2 </b>", "<b>@2</b> ");
        replacements.put("<b>3 </b>", "<b>@3</b> ");
        replacements.put("<b>4 </b>", "<b>@4</b> ");
        replacements.put("<b>5 </b>", "<b>@5</b> ");
        replacements.put("<b>6 </b>", "<b>@6</b> ");
        replacements.put("<b>7 </b>", "<b>@7</b> ");
        replacements.put("<b>8 </b>", "<b>@8</b> ");
        replacements.put("<b>9 </b>", "<b>@9</b> ");
        replacements.put("<b>10 </b>", "<b>@10</b> ");
        replacements.put("<b>11 </b>", "<b>@11</b> ");
        replacements.put("<b>12 </b>", "<b>@12</b> ");
        //
        replacements.put("<b>1 ", "<b>@1</b> <CL/><b>");
        replacements.put("<b>2 ", "<b>@2</b> <CL/><b>");
        replacements.put("<b>3 ", "<b>@3</b> <CL/><b>");
        replacements.put("<b>4 ", "<b>@4</b> <CL/><b>");
        replacements.put("<b>5 ", "<b>@5</b> <CL/><b>");
        replacements.put("<b>6 ", "<b>@6</b> <CL/><b>");
        replacements.put("<b>7 ", "<b>@7</b> <CL/><b>");
        replacements.put("<b>8 ", "<b>@8</b> <CL/><b>");
        replacements.put("<b>9 ", "<b>@9</b> <CL/><b>");
        replacements.put("<b>10 ", "<b>@10</b> <CL/><b>");
        replacements.put("<b>11 ", "<b>@11</b> <CL/><b>");
        replacements.put("<b>12 ", "<b>@12</b> <CL/><b>");


        //
        replacements.put(" \\]", "]");
        replacements.put("\\[ ", "[");
        replacements.put(" </b>\\[", "</b> [");
        replacements.put(" </b>", "</b> ");
        replacements.put("<custom>", "<h1>");
        replacements.put("</custom>", "</h1>");
        replacements.put("­", "-");
        replacements.put("</sup> </b>", "</sup></b> ");
        replacements.put(" \\,", "\\,");
        replacements.put("\\,</b> \\-<b>", ", -");
        replacements.put("´", "’");
        replacements.put("</b> / <b>", " @/ ");
        replacements.put("<i>\\. ", " .<i>");
        //
        replacements.put("<i>\\. ", " .<i>");
        replacements.put("\\%", ",");
        replacements.put("\\+ ", "+");
        replacements.put("@@", "@");

        replacements.put("«", "\"");// «aguinaldo»
        replacements.put("»", "\"");

        replacements.put("&sup1;", "<sup>1</sup>");
        replacements.put("&sup2;", "<sup>2</sup>");

        replacements.put("<a name=\"_GoBack\">", "");
        replacements.put("</a>", "");
    }

    @Override
    public void markLabels(Properties lblDict) {
        string = string.replaceAll("\\+", "</i>+<i>");
        string = string.replaceAll("</i></i>\\+<i><i>", "</i>+<i>");
        String content = string;
        try {
            //Сортируем массив помет
            List<String> list = new ArrayList<String>();
            for (String str : lblDict.stringPropertyNames()) {
                list.add(str);
            }
            Collections.sort(list, new LengthComparator());
            //Находим все пометы и заменяем их спецсиволами
            String tmp = "<entry>" + content + "</entry>";
            StringReader sr = new StringReader(tmp);
            HtmlPreparer.JAXBEntry en = JAXB.unmarshal(sr, HtmlPreparer.JAXBEntry.class);
            for (String lblInEntry : en.getItalic()) {
                boolean done = false;
                for (String lblInDict : list) {
                    if (lblInEntry.equalsIgnoreCase(lblInDict)) {
                        string = string.replaceFirst("<i>" + lblInEntry + "</i>", "<i>" + labelStartSpecChar + lblInEntry + labelSEndSpecChar + "</i>");
                        done = true;
                    }
                }
                Map<String, String> repls = new HashMap<String, String>();
                if (!done) {
                    String res = lblInEntry.trim();
                    if (checkAllIsLabel(list, lblInEntry)) {
                        for (String lblInDict : list) {
                            String target = "<i>{" + lblInDict + "}</i>";
                            String key = "" + System.nanoTime();
                            repls.put(key, target);
                            res = res.replace(lblInDict, key);
                        }
                        res = res.replaceAll(", ", "##");
                        res = res.replaceAll(" ", "#");
                        //res = "{" + res + "}";
                        string = string.replace("<i>" + lblInEntry + "</i>", res);
                    }
                }
                for (String key : repls.keySet()) {
                    string = string.replace(key, repls.get(key));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }

    private boolean checkAllIsLabel(List<String> list, String content) {
        for (String lblInDict : list) {
            content = content.replace(lblInDict, "");
        }
        content = content.replaceAll(",", "").trim();
        return content.isEmpty();
    }

    @Override
    public void clean() {
        super.clean();
        String tmp = string;
        tmp = tmp.replaceAll("<TEMPB style='color:aqua'>", "<b style='color:aqua'>");
        tmp = tmp.replaceAll("</b><b style\\=\\'color\\:aqua\\'>", "");
        tmp = tmp.replaceAll("<b style\\=\\'color\\:aqua\\'>\\.</b>", ".");
        tmp = tmp.replaceAll("</b> <b style\\=\\'color\\:aqua\\'>", " ");
        tmp = tmp.replaceAll(" ,", ",");
        tmp = portuEscapes(tmp);
        tmp = spanishEscapes(tmp);
        string = tmp;
    }

    private static String portuEscapes(String string) {
        String res = string;
        for (String s : portumap.keySet()) {
            res = res.replaceAll(s, portumap.get(s));
        }
        return res;
    }

    private static String spanishEscapes(String string) {
        String res = string;
        for (String s : spanishmap.keySet()) {
            res = res.replaceAll(s, spanishmap.get(s));
        }
        return res;
    }

    public static void main(String[] args) {
        //for (String s : portumap.values()) {
        System.out.println("d  '" + Utils.toUnicodeEscapeString("a") + "'|");
        System.out.println("en '" + Utils.toUnicodeEscapeString("a") + "'|");//en
        System.out.println("ru '" + Utils.toUnicodeEscapeString("а") + "'|");//en
        //}
    }
}
